#include "..\include\CCamera.h"

CCamera::CCamera(void)
{
}

CCamera::CCamera(CameraType camType) : m_CameraType(camType)
{
	m_pos.x = m_pos.y = m_pos.z = 0.0f;
	m_right.x = 1.0f;
	m_right.y = 0.0f;
	m_right.z = 0.0f;
	D3DXVec3Normalize(&m_right, &m_right);

	m_look.x = 0.0f;
	m_look.y = 0.0f;
	m_look.z = 1.0f;
	D3DXVec3Normalize(&m_look, &m_look);

	m_up.x = 0.0f;
	m_up.y = 1.0f;
	m_up.z = 0.0f;
	D3DXVec3Normalize(&m_up, &m_up);
}


CCamera::~CCamera(void)
{
}

void CCamera::Initialize(IDirect3DDevice9* device)
{
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &m_pos, &m_look, &m_up);

	device->SetTransform(D3DTS_VIEW, &V);
}

void CCamera::strafe(float fUnits)
{
	// move only on xz plane for land object
	if(m_CameraType == LANDOBJECT )
	   m_pos += D3DXVECTOR3(m_right.x, 0.0f, m_right.z) * fUnits;

	if(m_CameraType == AIRCRAFT )
	   m_pos += m_right * fUnits;
}

void CCamera::fly(float fUnits)
{
	if(m_CameraType == AIRCRAFT )
		m_pos += m_up * fUnits;
}

void CCamera::walk(float fUnits)
{
	// move only on xz plane for land object
	if(m_CameraType == LANDOBJECT )
	   m_pos += D3DXVECTOR3(m_look.x, 0.0f, m_look.z) * fUnits;

	if(m_CameraType == AIRCRAFT )
	   m_pos += m_look * fUnits;
}

void CCamera::pitch(float fAngle)
{
	D3DXMATRIX T;
	D3DXMatrixRotationAxis(&T, &m_right, fAngle);

	// rotate m_up and m_look around m_right vector
	D3DXVec3TransformCoord(&m_up,&m_up, &T);
	D3DXVec3TransformCoord(&m_look,&m_look, &T);
}

void CCamera::yaw(float fAngle)
{
D3DXMATRIX T;

	// rotate around world y (0, 1, 0) always for land object
	if(m_CameraType == LANDOBJECT )
	  D3DXMatrixRotationY(&T, fAngle);

	// rotate around own m_up vector for aircraft
	if(m_CameraType == AIRCRAFT )
	  D3DXMatrixRotationAxis(&T, &m_up, fAngle);

	// rotate m_right and m_look around m_up or y-axis
	D3DXVec3TransformCoord(&m_right,&m_right, &T);
	D3DXVec3TransformCoord(&m_look,&m_look, &T);
}

void CCamera::roll(float fAngle)
{
	// only roll for aircraft type
	if(m_CameraType == AIRCRAFT)
	{
	  D3DXMATRIX T;
	  D3DXMatrixRotationAxis(&T, &m_look, fAngle);

	  // rotate m_up and m_right around m_look vector
	  D3DXVec3TransformCoord(&m_right,&m_right, &T);
	  D3DXVec3TransformCoord(&m_up,&m_up, &T);
	}
}

void CCamera::GetViewMatrix(D3DMATRIX* v)
{
	D3DXVec3Normalize(&m_look, &m_look);

	D3DXVec3Cross(&m_up, &m_look, &m_right);
	D3DXVec3Normalize(&m_up, &m_up);

	D3DXVec3Cross(&m_right, &m_up, &m_look);
	D3DXVec3Normalize(&m_right, &m_right);

	float x = -D3DXVec3Dot(&m_right, &m_pos);
	float y = -D3DXVec3Dot(&m_up, &m_pos);
	float z = -D3DXVec3Dot(&m_look, &m_pos);

	(*v)._11 = m_right.x;
	(*v)._12= m_up.x;
	(*v)._13= m_look.x;
	(*v)._14= 0.0f;

	(*v)._21 = m_right.y;
	(*v)._22 = m_up.y;
	(*v)._23 = m_look.y;
	(*v)._24 = 0.0f;

	(*v)._31 = m_right.z;
	(*v)._32 = m_up.z;
	(*v)._33= m_look.z;
	(*v)._34 = 0.0f;

	(*v)._41 = x;
	(*v)._42 = y;
	(*v)._43 = z;
	(*v)._44 = 1.0f;
}